<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;
use Illuminate\Support\Facades\DB;

class Withdraw extends CxhshopBase {

    protected $table = 'ims_cxh_user_cash';

     protected $fillable = ['member_id', 'user_bank_id', 'uch_type', 'uch_money', 'uch_credit2', 'uch_credit4','uch_credit5', 'uch_poundage_money', 'uch_real_money'];

    public $timestamps = false;

    protected $dates = [];

    public static $rules = [
        // Validation rules
    ];
    /**
     * 写入提现记录及相关的日志记录
     * @param $memInfo 用户相关信息
     * @param $params 插入的数据
     * @return int
     */
    public function insertUserCash($memInfo,$params){
        DB::connection('jmft_cxh') -> beginTransaction();
        //往user_cash里面写数据
        $money = $params['money'];//提现金额
        $paytype = $params['paytype'] ? $params['paytype'] : 3;//支付方式：3：银行卡方式,默认银行卡
        $poundagemoney = $params['deductionmoney'];//提现手续费
        $realmoney = $params['realmoney'];//实际提现金额
        $idcard = $params['idcard'];//身份证信息
        $userBankId = $params['user_bank_id'];//提现银行卡id
        $credit2 = $params['credit2'];//从余额里面提现的金额
        $credit4 = $params['credit4'];//从里程金（车辆补贴）里面提现的金额
        $credit5 = $params['credit5'];//从代理商保证金（运营补贴）里面提现的金额
        $userCashData = [
            'uch_money'=>$money,
            'uch_credit2'=>$credit2,
            'uch_credit4'=>$credit4,
            'uch_credit5'=>$credit5,
            'uch_poundage_money'=>$poundagemoney,
            'uch_real_money'=>$realmoney,
            'uch_type'=>$paytype,
            'user_bank_id'=>$userBankId,
            'member_id'=>$memInfo['id'],
            'ctime'=>time(),
            'utime'=>time(),
        ];
        $query = self::query();
        $userCashId = $query -> insertGetId($userCashData);
        //往cashLog表里面写数据
        $cashLog = new CashLog();
        $returnData = $cashLog -> addCashLog($memInfo,$userCashId,$params);
        //修改用户表的信息
        $memSaveData = $returnData['memberSaveData'];
        $shopMember = new ShopMember();
        $memRes = $shopMember -> saveFields($memInfo['id'],$memSaveData);
        if($returnData['cashLogRe2'] > 0 && $returnData['cashLogRe4'] > 0 && $returnData['cashLogRe5'] > 0 && $memRes > 0){
            DB::connection('jmft_cxh') -> commit();
            $result = 1;
        }else{
            DB::connection('jmft_cxh') -> rollBack();
            $result = 0;
        }
        return $result;
    }

    /**
     * @param $memId  用户id
     * @return mixed
     */
    public function getRecords($memId,$page = 1,$limit = 10){
        $fields = ['uc.id','user_bank_id','uch_money','uch_status','uc.ctime','bank','bank_no'];
        $data =DB::connection('jmft_cxh')
            ->table('ims_cxh_user_cash as uc')
            ->leftjoin('ims_cxh_user_bank as ub', 'uc.user_bank_id', '=', 'ub.id')
            ->leftJoin('ims_cxh_bank as b', 'b.id', '=', 'ub.b_id')
            ->where('uc.member_id','=',$memId)
            ->offset($page)
            ->limit($limit)
            ->orderBy('uc.id','desc')
            ->get($fields);
        $list = json_decode($data,true);
        return $list;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 第二个参数为user_bank的id
     * 第三个参数为提现表与用户银行卡表对应的id
     */
    public function getBankInfo(){
        return $this->hasOne('App\Models\Cxhshop\UserBank','id','user_bank_id');
    }

    /**
     * 获取提现详情
     */
    public function getDetailById($id){
        $fields = ['bank','bank_no','user_name','uch_money','uch_poundage_money','uch_real_money','uch_status','uc.ctime','refuse_reason'];
        $data =DB::connection('jmft_cxh')
            ->table('ims_cxh_user_cash as uc')
            ->leftjoin('ims_cxh_user_bank as ub', 'uc.user_bank_id', '=', 'ub.id')
            ->leftJoin('ims_cxh_bank as b', 'b.id', '=', 'ub.b_id')
            ->where('uc.id','=',$id)
            ->get($fields);
        $list = json_decode($data,true);
        return $list;
    }

    /**
     * @param $memId
     * @param $status
     * @return int
     * 获取固定状态的提现记录：0待审核；1已通过；2已拒绝
     */
    public function getStatus0($memId,$status){
        $num =DB::connection('jmft_cxh')
            ->table('ims_cxh_user_cash')
            -> where('uch_status',$status)
            -> where('member_id',$memId)
            -> count();
        return $num;
    }
}